package cn.itcast.web.task;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.system.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.log4j.Log4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;


/**
 * 每天检查：是否三天之后有交付的购销合同
 * 获取该合同，将合同的数据发到RabbitMQ
 * EmailService(分布式服务),从RabbitMQ获取数据，用email
 * 模板，发送
 * 问题：如果是提前三天，涉不涉及三天内的送货日期进行
 *
 */
@Log4j
public class DeliveryNotificationTask {


    @Autowired
    private ContractDao contractDao;

    @Autowired
    private UserService userService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 获取满足通知要求的全部合同，发送邮件提醒
     */
    public void getAndSend(){

        //获取合同
        List<Contract> contractList = getContract();

        if(contractList.size()==0){
            log.debug("今天没有需要通知的合同");
            return;
        }

        for (Contract contract : contractList) {

            HashMap<String, Object> map = new HashMap<>();

            String contractNo = contract.getContractNo();

            //获取合同创建人的email
            User user = userService.findById(contract.getCreateBy());
            String receiver = user.getEmail();
            String userName = user.getUserName();

            map.put("email",receiver);
            map.put("subject","Delivery Notification");
            map.put("content","亲爱的Saas用户" +userName+
                    "你好，\n   您购销合同" + contractNo+
                    "下的货物3天后即将到运，请及时查收。谢谢使用SaasExport平台");

            //将map最终以JSON数据向RabbitMQ发送:queue:notification_queue
            rabbitTemplate.convertAndSend("saas_mail_exchange","delivery_mail",map);
        }

    }


    /**
     * 获得3天后的全部合同
     */
    public List<Contract> getContract(){

        //构建当天的日期
        Date now = new Date();

        Date start = getStart(now);
        Date end = getEnd(now);
        //查询第三天后的全部合同
        ContractExample e = new ContractExample();
        ContractExample.Criteria criteria = e.createCriteria();
        criteria.andDeliveryPeriodBetween(start, end);
        return contractDao.selectByExample(e);

    }


    /**
     *
     * @param now
     * @return
     */
    private Date getStart(Date now){

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(now);
        //获取3天后日期
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH) + 3);

        //时分秒清零
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);

        return calendar.getTime();
    }

    /**
     *
     * @param now
     * @return
     */
    private Date getEnd(Date now){

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(now);
        //获取3天后日期
        calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH) + 3);

        //时分秒清零
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        calendar.set(Calendar.MILLISECOND,999);

        return calendar.getTime();
    }





}
